/**
 * This controller manages the User details view which are added as tabs (so multiple
 * instances are created). Each instance of the view creates an instance of this class to
 * control its behavior.
 */
Ext.define('DCApp.view.building.DetailController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.buildingdetail',

    requires: [
        'Ext.window.Toast'
    ],
    config: {
        control: {
            'button[text=插入]': {
                click: 'onInsert'
            }
        }
    },
    onBackClick: function() {
    	 this.redirectTo("buildinglist")
    },    
    onSaveClick: function() {
        var form = this.lookupReference('form'),
            rec;
        
        if (form.isValid()) {
            rec = this.getViewModel().get('theObj');
            Ext.Msg.wait('保存', '保存古建中...');
            rec.save({
                scope: this,
                callback: this.onComplete
            });
        }
    },

    onComplete: function() {
        Ext.Msg.hide();
        Ext.toast({
            title: '保存',
            html: '古建保存完成',
            align: 't',
            bodyPadding: 10
        });
        this.redirectTo("buildinglist");
    },
    
    onUploadBuilding: function() {
    	var vw = this.getView();
		Ext.MessageBox.confirm('确认覆盖', '需要将原始的文本提取到正文框吗?',
		function(btn, text){
			if(btn!='yes')
				return false;
    		var he = vw.down('htmleditor');
    		he.setValue("提取的纯文本1234");
		}, this);
    },
    
    onInsert:function(btn,e){
    	var vw = this.getView();
    	var he = vw.down('htmleditor');
    	var dv = btn.up('panel').down('dataview');
    	var sels = dv.getSelectionModel().getSelection();
    	var text="";
    	for(var i=0; i<sels.length; i++){
    		var url = sels[i].get('url');
    		if(url.indexOf('.jpg')!=-1)
    			text+='<img src="'+url+'"/>';
    		else
    			text+='<a href="'+url+'">'+ sels[i].get('name')+'</a>';
    	}
    	he.insertAtCursor(text);
    	he.focus();
    }
    
});
